﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJqGrid;
using PagedList;
using WebApp.Models;
using WebApp.Services;
using WebApp.Data;

namespace WebApp.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }

        private CustomerService _service = null;

        public JsonResult GetCustomers(GridSettings gridSettings, string keyword)
        {
            _service = new CustomerService();

            if (string.IsNullOrEmpty(keyword))
                keyword = string.Empty;
            var realCount = 0;
            var datas = _service.GetCustomers(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);
            //var datas = _service.GetEmployees(ref realCount);

            var lists = new List<CustomerModel>();
            foreach (var model in datas)
            {
                var item = new CustomerModel();
                item.CustomerId = model.customer_id;
                item.CustomerFirstName = model.customer_firstname;
                item.CustomerLastName = model.customer_lastname;
                item.CustomerCompany = model.customer_company;
                item.CustomerAddress = model.customer_address;
                item.CustomerTel = model.customer_tel;
                item.CustomerFax = model.customer_fax;
                item.CustomerEmail = model.customer_email;
                item.CustomerType = model.customer_type;
                //item.CustomerCreateBy = model.customer_create_by;
                item.CustomerCreateDate = model.customer_create_date.HasValue ? model.customer_create_date.Value : DateTime.Now;
                item.CustomerUpdateDate = model.customer_update_date.HasValue ? model.customer_update_date.Value : DateTime.Now;
                //item.CustomerUpdateBy = "";
                lists.Add(item);
            }

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from e in lists
                            select new
                            {
                                id = e.CustomerId,
                                cell = new[] {
                                            e.CustomerId.ToString(),
                                            e.CustomerFullName,
                                            e.CustomerFirstName,
                                            e.CustomerLastName,
                                            e.CustomerCompany,
                                            e.CustomerAddress,
                                            e.CustomerTel,
                                            e.CustomerFax,
                                            e.CustomerEmail,
                                            e.CustomerType,
                                            e.CustomerCreateDate.ToLongDateString(),
                                            
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Updates(tbl_customer cust, string oper)
        {
            _service = new CustomerService();

            var isPass = false;
            var iMessage = string.Empty;
            if (string.IsNullOrEmpty(oper))
            {
                iMessage = "oper is null";
                return Json(new { isPass, iMessage });
            }
            var operation = oper;

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                _service.UpdateCustomer(cust);
            }
            else if (operation.Equals("del"))
            {
                _service.DeleteCustomer(cust);
            }
            isPass = true;
            return Json(new { isPass, iMessage });
        }

        public JsonResult Update(tbl_customer cust, FormCollection formCollection)
        {
            _service = new CustomerService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                _service.UpdateCustomer(cust);
            }
            else if (operation.Equals("del"))
            {
                _service.DeleteCustomer(cust);
            }
            isPass = true;
            return Json(new { isPass, iMessage });
        }
    }
}
